package logic

import (
	"context"
	"errors"
	"fmt"
	"video-guard/models/mysql"

	"video-guard/videorpc/internal/svc"
	"video-guard/videorpc/videorpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type CollectAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCollectAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CollectAddLogic {
	return &CollectAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CollectAddLogic) CollectAdd(in *videorpc.CollectAddTRequest) (*videorpc.CollectAddTResponse, error) {
	// todo: add your logic here and delete this line
	var user mysql.UserInfo
	err := user.FindUserById(mysql.DB, in.UserId)
	if err != nil {
		return nil, errors.New("未登录")
	}

	var collect mysql.CollectInfo
	collect.FindCollect(mysql.DB, in.UserId, in.VideoId)
	if collect.ID != 0 {
		return nil, errors.New("已收藏该视频")
	}
	collect = mysql.CollectInfo{
		VideoId: in.VideoId,
		UserId:  in.UserId,
	}
	fmt.Println(collect.ID)
	err = collect.CollectAdd(mysql.DB)
	if err != nil {
		return nil, errors.New("添加失败")
	}
	fmt.Println(collect.ID)
	return &videorpc.CollectAddTResponse{
		CollectId: int64(collect.ID),
	}, nil
}
